library(data.table)
library(tidyr)
library(xts)
library(gtools)

data_dir <- "~/Desktop/Research/lobster/"
save_dir <- "~/Disk/Research/image_support_test/"
    
# all_files <- list.files(data_dir)
# all_files <- all_files[grep("_34200000_57600000_", all_files)]  
# all_files <- strsplit(all_files, "_34200000_57600000_")
# all_files <- sort(unique(sapply(all_files, function(x) x[1])))



all_files = c( "AAPL_2019-01-02","AAPL_2019-01-03","AAPL_2019-01-04","AAPL_2019-01-07","AAPL_2019-01-08",  
               "AAPL_2019-01-10", "AAPL_2019-01-11","AAPL_2019-01-14","AAPL_2019-01-15","AAPL_2019-01-16", 
               "AAPL_2019-01-17", "AAPL_2019-01-18","AAPL_2019-01-22","AAPL_2019-01-23","AAPL_2019-01-24", 
               "AAPL_2019-01-25", "AAPL_2019-01-28","AAPL_2019-01-29","AAPL_2019-01-30","AAPL_2019-01-31")
support_max = 20
delta = 0.25
support_str = paste0("_support", toString(support_max),"delta", toString(delta))
config_str = "_state_hawkes_time_customizedtwo_LASSO_0005"
#config_str = "_state_hawkes_time_customizedtwo_LASSO_001"

######## time factor labels for model_config: state_hawkes_timecustomizedtwo ################
time_1 = seq(ISOdatetime(2019,1,2,9,31,0), ISOdatetime(2019,1,2,10,0,0), by=60)
time_2 = seq(ISOdatetime(2019,1,2,10,1,0), ISOdatetime(2019,1,2,15,30,0), by=60*5)
time_3 = seq(ISOdatetime(2019,1,2,15,31,0), ISOdatetime(2019,1,2,16,0,0), by=60)
time_label = c(time_1, time_2, time_3)
time_label = unlist(lapply(time_label, function(x){substr(as.character(x), 12,16)}))

  
#box_kernel_var_list <- list()
hawkes_markov_pref_price_list <- list()
#hawkes_markov_pref_price_eventsize_list <- list()


for (data_file_name in all_files){
  hawkes_markov_pref_price <- try(readRDS(paste0(save_dir, "data_agg_nosize/hawkes_markov_pref_price_", data_file_name, support_str, config_str,".rds")))
#  hawkes_markov_pref_price_eventsize <- try(readRDS(paste0(save_dir, "/hawkes_markov_pref_price_eventsize_", data_file_name, ".rds")))
  
  if(class(hawkes_markov_pref_price)[1] != "try-error"){
    hawkes_markov_pref_price_list[[data_file_name]] <- hawkes_markov_pref_price
  }
  # if(class(hawkes_markov_pref_price_eventsize)[1] != "try-error"){
  #   hawkes_markov_pref_price_eventsize_list[[data_file_name]] <- hawkes_markov_pref_price_eventsize
  # }
}

#load("../data/ody3.rda")
source("~/Desktop/Research/R/support_test/hawkes_utilities.R")


data_dir <- "~/Desktop/Research/lobster/"
save_dir <- "~/Downloads/Scaled_Plots"

#ticker <- "AAPL"
#hawkes_list = hawkes_markov_pref_price_list

aggregate_hawkes <- function(ticker, hawkes_list){
  ticker_file_name <- names(hawkes_list)
  ticker_file_name <- ticker_file_name[grep(ticker, ticker_file_name)]
  
  has_na <- sapply(hawkes_list[ticker_file_name], function(hawkes)
    any(is.na(hawkes$hawkes_kernel_est[grep("design_matrix", rownames(hawkes$hawkes_kernel_est)),])))
  ticker_file_name <- ticker_file_name[!has_na]
  
  hawkes_kernel <- sapply(hawkes_list[ticker_file_name], function(hawkes)
    hawkes$hawkes_kernel_est[grep("design_matrix", rownames(hawkes$hawkes_kernel_est)),],
    simplify = "array")
  
  hawkes_kernel <- apply(hawkes_kernel, 1:2, mean)
  markovian_kernel <- lapply(hawkes_list[ticker_file_name], function(hawkes)
    hawkes$hawkes_kernel_est[!grepl("design_matrix", rownames(hawkes$hawkes_kernel_est)),])
# Remove the first "price" factor since it is mannually assinged to 0 in categorial regression.  
  for (i in 1:length(names(markovian_kernel))){
    remove = rownames(markovian_kernel[[i]])[grep("price",rownames(markovian_kernel[[i]]))][1]
    print(remove)
    markovian_kernel[[i]] = markovian_kernel[[i]][!rownames(markovian_kernel[[i]]) %in% remove, ]
  }
  
  
  all_states <- mixedsort(unique(unlist(lapply(markovian_kernel, rownames))))
  markovian_kernel_mat <- array(NA, dim=c(length(all_states), ncol(markovian_kernel[[1]]), length(markovian_kernel)))
  dimnames(markovian_kernel_mat)[[1]] <- all_states
  dimnames(markovian_kernel_mat)[[2]] <- colnames(markovian_kernel[[1]])
  dimnames(markovian_kernel_mat)[[3]] <- names(markovian_kernel)
  for(i in names(markovian_kernel)){
    markovian_kernel_mat[rownames(markovian_kernel[[i]]),,i] <- markovian_kernel[[i]]
  }
  markovian_kernel_mat <- apply(markovian_kernel_mat, 1:2, mean, na.rm=TRUE)
  
  hawkes_markov <- list(
    delta=hawkes_list[[ticker_file_name[1]]]$delta,
    support_max=hawkes_list[[ticker_file_name[1]]]$support_max
  )
  hawkes_markov$hawkes_kernel_est <- rbind(markovian_kernel_mat, hawkes_kernel)
  hawkes_markov
}

data_file_name <- "AAPL"

hawkes_markov_pref_price <- aggregate_hawkes(data_file_name, hawkes_markov_pref_price_list)
# hawkes_markov_pref_price_eventsize <- aggregate_hawkes(data_file_name, hawkes_markov_pref_price_eventsize_list,
#                                                        hawkes_markov_adj_r_eventsize_list)

hawkes_kernel_est = hawkes_markov_pref_price$hawkes_kernel_est
old_rownames = c("design_matrix+1(+)", "design_matrix+1(-)","design_matrix+1(t)","design_matrix+2(+)", "design_matrix+2(-)","design_matrix+2(t)","design_matrix+3(+)", "design_matrix+3(-)","design_matrix+3(t)",
                 "design_matrix-1(+)", "design_matrix-1(-)","design_matrix-1(t)","design_matrix-2(+)", "design_matrix-2(-)","design_matrix-2(t)","design_matrix-3(+)", "design_matrix-3(-)","design_matrix-3(t)",
                 "design_matrixp+(t)", "design_matrixp+(-)","design_matrixp+(+)","design_matrixp-(t)", "design_matrixp-(-)","design_matrixp-(+)")
new_ronames = c("design_matrix+1(i)", "design_matrix+1(c)","design_matrix+1(t)","design_matrix+2(i)", "design_matrix+2(c)","design_matrix+2(t)","design_matrix+3(i)", "design_matrix+3(c)","design_matrix+3(t)",
                "design_matrix-1(i)", "design_matrix-1(c)","design_matrix-1(t)","design_matrix-2(i)", "design_matrix-2(c)","design_matrix-2(t)","design_matrix-3(i)", "design_matrix-3(c)","design_matrix-3(t)",
                "design_matrixp+(t)", "design_matrixp+(c)","design_matrixp+(i)","design_matrixp-(t)", "design_matrixp-(c)","design_matrixp-(i)")
for (i in 1:length(old_rownames)){
  rownames(hawkes_kernel_est)[rownames(hawkes_kernel_est)==old_rownames[i]] <- new_ronames[i] 
}
cols_all <- paste0(rep(sprintf("%+d", c(1:3, -(1:3))), each=3), "(", c("i", "c", "t"), ")")
cols_all <- c(cols_all, c("p+(i)", "p+(c)", "p+(t)", "p-(i)", "p-(c)", "p-(t)"))
colnames(hawkes_kernel_est) = cols_all

mu_label = c("0-100", "100-200","200-300","300-400","400-500",
             "500-600", "600-700","700-800","800-900","900+")

hawkes_markov_pref_price$hawkes_kernel_est <- hawkes_kernel_est

support_str = paste0("_support",toString(support_max), "delta", toString(delta))
pdf(paste0(save_dir, "/hawkes_markov_pref_price_kernel_",data_file_name,support_str,config_str,"_Scaled.pdf"),
    width = 5*ncol(hawkes_markov_pref_price$hawkes_kernel_est), height = 5*ncol(hawkes_markov_pref_price$hawkes_kernel_est))
plot_hawkes(hawkes_markov_pref_price)
for(i in 1:ncol(hawkes_markov_pref_price$hawkes_kernel_est)){
  mu <- hawkes_markov_pref_price$hawkes_kernel_est[grep("state", rownames(hawkes_markov_pref_price$hawkes_kernel_est)), i]
#  barplot(mu, las=2, main=paste0("Liquidity State for Event: ",colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i]), names.arg = mu_label)
  par(mai=c(1.5,1,1,1))
  barplot(mu, las=2, main=paste0("Liquidity State for Event: ",colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i]),names.arg = mu_label, xlab = "", ylab = "Liquidty State")
  mtext(side = 1, text = "Liquidity State Category", line = 5)
}
for(i in 1:ncol(hawkes_markov_pref_price$hawkes_kernel_est)){
  mu <- hawkes_markov_pref_price$hawkes_kernel_est[grep("time", rownames(hawkes_markov_pref_price$hawkes_kernel_est)), i]
  if (config_str == "_state_hawkes_time_customizedtwo"||config_str == "_state_hawkes_time_customizedtwo_LASSO"||config_str == "_state_hawkes_time_customizedtwo_LASSO_0005"||config_str == "_state_hawkes_time_customizedtwo_LASSO_001"){
#    barplot(mu, las=2,main=paste0("Time Factor for Event: ",colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i]),names.arg = time_label)
    plot((mu-mean(mu)), type = "l", xaxt = "n", main=paste0("Time Factor for Event: ",colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i]),xlab = "", ylab = "Time Factor")
    mtext(side = 1, text = "Time", line = 4)
    axis(1, at=c(seq(1,126,9),126), labels=time_label[c(seq(1,126,9),126)], las = 2)
  }else{  barplot(mu, las=2, main=paste0("Time Factor for Event: ",colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i]))}

}

dev.off()

pdf(paste0(save_dir, "/hawkes_markov_pref_price_kernel_individual_",data_file_name,support_str,config_str,"_Scaled.pdf"),
    width = 6, height = 5)
plot_hawkes(hawkes_markov_pref_price,matrix_layout=FALSE)
for(i in 1:ncol(hawkes_markov_pref_price$hawkes_kernel_est)){
  mu <- hawkes_markov_pref_price$hawkes_kernel_est[grep("state", rownames(hawkes_markov_pref_price$hawkes_kernel_est)), i]
  #  barplot(mu, las=2, main=paste0("Liquidity State for Event: ",colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i]), names.arg = mu_label)
  par(mai=c(1.5,1,1,1))
  barplot(mu, las=2, main=paste0("Liquidity State for Event: ",colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i]),names.arg = mu_label, xlab = "", ylab = "Liquidty State")
  mtext(side = 1, text = "Liquidity State Category", line = 5)
}
for(i in 1:ncol(hawkes_markov_pref_price$hawkes_kernel_est)){
  mu <- hawkes_markov_pref_price$hawkes_kernel_est[grep("time", rownames(hawkes_markov_pref_price$hawkes_kernel_est)), i]
  if (config_str == "_state_hawkes_time_customizedtwo"||config_str == "_state_hawkes_time_customizedtwo_LASSO"||config_str == "_state_hawkes_time_customizedtwo_LASSO_0005"||config_str == "_state_hawkes_time_customizedtwo_LASSO_001"){
#    barplot(mu, las=2,main=paste0("Time Factor for Event: ",colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i]),names.arg = time_label)
    plot((mu-mean(mu)), type = "l", xaxt = "n", main=paste0("Time Factor for Event: ",colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i]),xlab = "", ylab = "Time Factor")
    mtext(side = 1, text = "Time", line = 4)
    axis(1, at=c(seq(1,126,9),126), labels=time_label[c(seq(1,126,9),126)], las = 2)
  }else{  barplot(mu, las=2, main=paste0("Time Factor for Event: ",colnames(hawkes_markov_pref_price$hawkes_kernel_est)[i]))}
  
}
dev.off()

################## Eventsize #######################
library(data.table)
library(tidyr)
library(xts)
library(gtools)

data_dir <- "~/Desktop/Research/lobster/"
save_dir <- "~/Disk/Research/image_support_test/"

# all_files <- list.files(data_dir)
# all_files <- all_files[grep("_34200000_57600000_", all_files)]
# all_files <- strsplit(all_files, "_34200000_57600000_")
# all_files <- sort(unique(sapply(all_files, function(x) x[1])))



all_files = c("AAPL_2019-01-02", "AAPL_2019-01-03","AAPL_2019-01-04","AAPL_2019-01-07","AAPL_2019-01-08",  
              "AAPL_2019-01-10", "AAPL_2019-01-11","AAPL_2019-01-14","AAPL_2019-01-15","AAPL_2019-01-16", 
              "AAPL_2019-01-17", "AAPL_2019-01-18","AAPL_2019-01-22","AAPL_2019-01-23","AAPL_2019-01-24", 
              "AAPL_2019-01-25", "AAPL_2019-01-28","AAPL_2019-01-29","AAPL_2019-01-30","AAPL_2019-01-31")
support_max = 20
delta = 0.25
support_str = paste0("_support", toString(support_max),"delta", toString(delta))
config_str = "_state_hawkes_time_customizedtwo_LASSO_0005"
#config_str = "_hawkes_LASSO_001"
######## time factor labels for model_config: state_hawkes_timecustomizedtwo ################
time_1 = seq(ISOdatetime(2019,1,2,9,31,0), ISOdatetime(2019,1,2,10,0,0), by=60)
time_2 = seq(ISOdatetime(2019,1,2,10,1,0), ISOdatetime(2019,1,2,15,30,0), by=60*5)
time_3 = seq(ISOdatetime(2019,1,2,15,31,0), ISOdatetime(2019,1,2,16,0,0), by=60)
time_label = c(time_1, time_2, time_3)
time_label = unlist(lapply(time_label, function(x){substr(as.character(x), 12,16)}))

hawkes_markov_pref_price_eventsize_list <- list()


for (data_file_name in all_files){
  hawkes_markov_pref_price_eventsize <- try(readRDS(paste0(save_dir, "data_agg_withsize/hawkes_markov_pref_price_eventsize_", data_file_name, support_str, config_str,".rds")))
  
  if(class(hawkes_markov_pref_price_eventsize)[1] != "try-error"){
    hawkes_markov_pref_price_eventsize_list[[data_file_name]] <- hawkes_markov_pref_price_eventsize
  }
}

#load("../data/ody3.rda")
source("~/Desktop/Research/R/support_test/hawkes_utilities.R")


data_dir <- "~/Desktop/Research/lobster/"
save_dir <- "~/Downloads/Scaled_Plots/"
aggregate_hawkes <- function(ticker, hawkes_list){
  ticker_file_name <- names(hawkes_list)
  ticker_file_name <- ticker_file_name[grep(ticker, ticker_file_name)]
  
  has_na <- sapply(hawkes_list[ticker_file_name], function(hawkes)
    any(is.na(hawkes$hawkes_kernel_est[grep("design_matrix", rownames(hawkes$hawkes_kernel_est)),])))
  ticker_file_name <- ticker_file_name[!has_na]
  
  hawkes_kernel <- sapply(hawkes_list[ticker_file_name], function(hawkes)
    hawkes$hawkes_kernel_est[grep("design_matrix", rownames(hawkes$hawkes_kernel_est)),],
    simplify = "array")
  
  hawkes_kernel <- apply(hawkes_kernel, 1:2, mean)
  markovian_kernel <- lapply(hawkes_list[ticker_file_name], function(hawkes)
    hawkes$hawkes_kernel_est[!grepl("design_matrix", rownames(hawkes$hawkes_kernel_est)),])
  # Remove the first "price" factor since it is mannually assinged to 0 in categorial regression.  
  for (i in 1:length(names(markovian_kernel))){
    remove = rownames(markovian_kernel[[i]])[grep("price",rownames(markovian_kernel[[i]]))][1]
    print(remove)
    markovian_kernel[[i]] = markovian_kernel[[i]][!rownames(markovian_kernel[[i]]) %in% remove, ]
  }
  
  
  all_states <- mixedsort(unique(unlist(lapply(markovian_kernel, rownames))))
  markovian_kernel_mat <- array(NA, dim=c(length(all_states), ncol(markovian_kernel[[1]]), length(markovian_kernel)))
  dimnames(markovian_kernel_mat)[[1]] <- all_states
  dimnames(markovian_kernel_mat)[[2]] <- colnames(markovian_kernel[[1]])
  dimnames(markovian_kernel_mat)[[3]] <- names(markovian_kernel)
  for(i in names(markovian_kernel)){
    markovian_kernel_mat[rownames(markovian_kernel[[i]]),,i] <- markovian_kernel[[i]]
  }
  markovian_kernel_mat <- apply(markovian_kernel_mat, 1:2, mean, na.rm=TRUE)
  
  hawkes_markov <- list(
    delta=hawkes_list[[ticker_file_name[1]]]$delta,
    support_max=hawkes_list[[ticker_file_name[1]]]$support_max
  )
  hawkes_markov$hawkes_kernel_est <- rbind(markovian_kernel_mat, hawkes_kernel)
  hawkes_markov
}

data_file_name <- "AAPL"

hawkes_markov_pref_price_eventsize <- aggregate_hawkes(data_file_name, hawkes_markov_pref_price_eventsize_list)

hawkes_kernel_est = hawkes_markov_pref_price_eventsize$hawkes_kernel_est
old_rownames = c("design_matrix+1(+)", "design_matrix+1(-)","design_matrix+1(t)","design_matrix+2(+)", "design_matrix+2(-)","design_matrix+2(t)","design_matrix+3(+)", "design_matrix+3(-)","design_matrix+3(t)",
                 "design_matrix-1(+)", "design_matrix-1(-)","design_matrix-1(t)","design_matrix-2(+)", "design_matrix-2(-)","design_matrix-2(t)","design_matrix-3(+)", "design_matrix-3(-)","design_matrix-3(t)",
                 "design_matrixp+(t)", "design_matrixp+(-)","design_matrixp+(+)","design_matrixp-(t)", "design_matrixp-(-)","design_matrixp-(+)")
new_ronames = c("design_matrix+1(i)", "design_matrix+1(c)","design_matrix+1(t)","design_matrix+2(i)", "design_matrix+2(c)","design_matrix+2(t)","design_matrix+3(i)", "design_matrix+3(c)","design_matrix+3(t)",
                "design_matrix-1(i)", "design_matrix-1(c)","design_matrix-1(t)","design_matrix-2(i)", "design_matrix-2(c)","design_matrix-2(t)","design_matrix-3(i)", "design_matrix-3(c)","design_matrix-3(t)",
                "design_matrixp+(t)", "design_matrixp+(c)","design_matrixp+(i)","design_matrixp-(t)", "design_matrixp-(c)","design_matrixp-(i)")
for (i in 1:length(old_rownames)){
  rownames(hawkes_kernel_est)[rownames(hawkes_kernel_est)==old_rownames[i]] <- new_ronames[i] 
}
cols_all <- paste0(rep(sprintf("%+d", c(1:3, -(1:3))), each=3), "(", c("i", "c", "t"), ")")
cols_all <- c(cols_all, c("p+(i)", "p+(c)", "p+(t)", "p-(i)", "p-(c)", "p-(t)"))
colnames(hawkes_kernel_est) = cols_all
mu_label = c("0-100", "100-200","200-300","300-400","400-500",
             "500-600", "600-700","700-800","800-900","900+")

hawkes_markov_pref_price_eventsize$hawkes_kernel_est <- hawkes_kernel_est

support_str = paste0("_support",toString(support_max), "delta", toString(delta))


pdf(paste0(save_dir, "/hawkes_markov_pref_price_eventsize_kernel_",data_file_name,support_str,config_str,"_Scaled_enlarged_xbar.pdf"),
    width = 5*ncol(hawkes_markov_pref_price_eventsize$hawkes_kernel_est),
    height = 5*ncol(hawkes_markov_pref_price_eventsize$hawkes_kernel_est))
plot_hawkes_new(hawkes_markov_pref_price_eventsize)
for(i in 1:ncol(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)){
  mu <- hawkes_markov_pref_price_eventsize$hawkes_kernel_est[grep("state", rownames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)), i]
#  barplot(mu, las=2, main=paste0("Liquidity State for Event: ",colnames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)[i]),names.arg = mu_label)
  par(mai=c(1.5,1,1,1))
  barplot(mu, las=2, main=paste0("Liquidity State for Event: ",colnames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)[i]),names.arg = mu_label, xlab = "", ylab = "Liquidty State")
  mtext(side = 1, text = "Liquidity State Category", line = 5)
}
for(i in 1:ncol(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)){
  mu <- hawkes_markov_pref_price_eventsize$hawkes_kernel_est[grep("time", rownames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)), i]
  if (config_str == "_state_hawkes_time_customizedtwo"||config_str == "_state_hawkes_time_customizedtwo_LASSO"||config_str == "_state_hawkes_time_customizedtwo_LASSO_0005"||config_str == "_state_hawkes_time_customizedtwo_LASSO_001"){
#    barplot(mu, las=2, main=paste0("Time Factor for Event: ",colnames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)[i]), names.arg = time_label)
    plot((mu-mean(mu)), type = "l", xaxt = "n", main=paste0("Time Factor for Event: ",colnames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)[i]),xlab = "", ylab = "Time Factor")
    mtext(side = 1, text = "Time", line = 4)
    axis(1, at=c(seq(1,126,9),126), labels=time_label[c(seq(1,126,9),126)], las = 2)
  }else{  barplot(mu, las=2, main=colnames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)[i])}
  
}
dev.off()

pdf(paste0(save_dir, "/hawkes_markov_pref_price_eventsize_kernel_individual_",data_file_name,support_str,config_str,"_Scaled_enlarged_xbar.pdf"),
width =6 ,height = 5)
plot_hawkes_new(hawkes_markov_pref_price_eventsize,matrix_layout=FALSE)
for(i in 1:ncol(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)){
  mu <- hawkes_markov_pref_price_eventsize$hawkes_kernel_est[grep("state", rownames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)), i]
#  barplot(mu, las=2, main=paste0("Liquidity State for Event: ",colnames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)[i]),names.arg = mu_label)
  par(mai=c(1.5,1,1,1))
  barplot(mu, las=2, main=paste0("Liquidity State for Event: ",colnames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)[i]),names.arg = mu_label, xlab = "", ylab = "Liquidty State")
  mtext(side = 1, text = "Liquidity State Category", line = 5)
}
for(i in 1:ncol(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)){
  mu <- hawkes_markov_pref_price_eventsize$hawkes_kernel_est[grep("time", rownames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)), i]
  if (config_str == "_state_hawkes_time_customizedtwo"||config_str == "_state_hawkes_time_customizedtwo_LASSO"||config_str == "_state_hawkes_time_customizedtwo_LASSO_0005"||config_str == "_state_hawkes_time_customizedtwo_LASSO_001"){
#    barplot(mu, las=2, main=paste0("Time Factor for Event: ",colnames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)[i]), names.arg = time_label)
    plot((mu-mean(mu)), type = "l", xaxt = "n", main=paste0("Time Factor for Event: ",colnames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)[i]),xlab = "", ylab = "Time Factor")
    mtext(side = 1, text = "Time", line = 4)
    axis(1, at=c(seq(1,126,9),126), labels=time_label[c(seq(1,126,9),126)], las = 2)
  }else{  barplot(mu, las=2, main=colnames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)[i])}
  
}
dev.off()



# pdf(paste0(save_dir, "/hawkes_markov_pref_price_eventsize_kernel_individual_",data_file_name,".pdf"),
#     width = 5, height = 5)
# plot_hawkes(hawkes_markov_pref_price_eventsize, matrix_layout=FALSE)
# for(i in 1:ncol(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)){
#   mu <- hawkes_markov_pref_price_eventsize$hawkes_kernel_est[grep("state", rownames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)), i]
#   barplot(mu, las=2, main=colnames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)[i])
# }
# for(i in 1:ncol(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)){
#   mu <- hawkes_markov_pref_price_eventsize$hawkes_kernel_est[grep("price", rownames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)), i]
#   barplot(mu, las=2, main=colnames(hawkes_markov_pref_price_eventsize$hawkes_kernel_est)[i])
# }
# a = hawkes_markov_pref_price_eventsize$adj_r
# for (i in 1:nrow(hawkes_markov_pref_price$adj_r)){
#   barplot(unlist(a[i,2:ncol(a)]), las = 2, main =paste0(toString(a[i,.id]),
#                                                         " Adjusted R Squared, mean over all bars=",  round(mean(unlist(a[i,2:ncol(a)])),4)))
# }
# 
# barplot( apply(a[,2:ncol(a)], 2, mean), las =2, main = paste0("Mean Adjusted R squared over all days, mean over all bars=",
#                                                               round(mean(apply(a[,2:ncol(a)], 2, mean)), 4)     )  )
# dev.off()


